package com.warmlight.voicepacket.widget;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AlertDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.umeng.analytics.MobclickAgent;
import com.warmlight.voicepacket.AllPlayerAcitivity;
import com.warmlight.voicepacket.PlayDeatalActivity;
import com.warmlight.voicepacket.R;
import com.warmlight.voicepacket.RecordActivity;
import com.warmlight.voicepacket.TextToVoiceActivity;
import com.warmlight.voicepacket.adapter.RecommendRVAdapter;
import com.warmlight.voicepacket.adapter.TopicBannerPagerAdapter;
import com.warmlight.voicepacket.data.RecommendData;
import com.warmlight.voicepacket.ui.imlm.GoodListenerTeaseActivity;
import com.warmlight.voicepacket.ui.user.LoginRegistActivity;
import com.warmlight.voicepacket.utils.MobClickHelper;
import com.warmlight.voicepacket.utils.SPUtils;
import com.zhuge.analysis.stat.ZhugeSDK;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static com.warmlight.voicepacket.db.VoicePacketDBHelper.LIST_ID;

/**
 * Created by jason on 2018/5/30.
 */

public class RecommendHeader extends RelativeLayout implements View.OnClickListener {

    private Context mContext;
    private RelativeLayout mContainer;
//    private HorizontalListView hlv_recommend;
    public RecyclerView rv_recommend;
    private BannerTopic bn_recommend;
    private RecommendRVAdapter mAdapter;
    private TopicBannerPagerAdapter mBannerAdapter;
    private String category_id;
    private LinearLayout ll_nav_4;
    //    private RecommendHeaderAdapter mRecommendHeaderAdapter;

    public RecommendHeader(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public RecommendHeader(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public RecommendHeader(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RecommendHeader(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    private void init(){
        mContainer = (RelativeLayout) View.inflate(mContext, R.layout.header_recommend, this);
//        hlv_recommend = (EventHListView) mContainer.findViewById(R.id.hlv_recommend);
        //更多
        RelativeLayout rl_hot_more = (RelativeLayout) mContainer.findViewById(R.id.rl_hot_more);
        rl_hot_more.setOnClickListener(this);
        rv_recommend = (RecyclerView) mContainer.findViewById(R.id.rv_recommend);
        LinearLayoutManager llm = new LinearLayoutManager(mContext);
        llm.setOrientation(LinearLayoutManager.HORIZONTAL);
        rv_recommend.setLayoutManager(llm);
        mAdapter = new RecommendRVAdapter(mContext,new ArrayList<RecommendData.DataBean.PlayCoversBean>());
        rv_recommend.setAdapter(mAdapter);

        //TODO 初始化banner
        bn_recommend = (BannerTopic) mContainer.findViewById(R.id.bn_recommend);
        mBannerAdapter = bn_recommend.getBannerAdapter();
//750 256
        int width = mContext.getResources().getDisplayMetrics().widthPixels;
        int height = 256 * width/750;
        ViewGroup.LayoutParams layoutParams = bn_recommend.getLayoutParams();
        layoutParams.height = height;
        bn_recommend.setLayoutParams(layoutParams);

        ll_nav_4 = (LinearLayout) mContainer.findViewById(R.id.ll_nav_4);
        ll_nav_4.setVisibility(GONE);
//        List<RecommendData.DataBean.PlayCoversBean> list = new ArrayList<>();
//        RecommendData.DataBean.PlayCoversBean bean = new RecommendData.DataBean.PlayCoversBean();
//        bean.setId("0");
//        bean.setName("全部播单");
//        bean.setPic("");
//        list.add(bean);
//
//        mRecommendHeaderAdapter = new RecommendHeaderAdapter(mContext, list);
//        hlv_recommend.setAdapter(mRecommendHeaderAdapter);
    }

    public RecommendRVAdapter getAdapter(){
        return mAdapter;
    }

    public TopicBannerPagerAdapter getBannerAdapter(){
        return mBannerAdapter;
    }

    public void setBannerVisible(boolean b) {
        bn_recommend.setVisibility(b? VISIBLE:GONE);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.rl_hot_more:
                Intent intentList = new Intent(mContext, AllPlayerAcitivity.class);
                if (TextUtils.isEmpty(category_id)){
                    category_id = "";
                }
                intentList.putExtra("category_id",category_id);
                intentList.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                //所有播单入口
//                MobclickAgent.onEvent(mContext.getApplicationContext(), MobClickHelper.DIS_BAG_ALL);
//                ZhugeSDK.getInstance().track(mContext.getApplicationContext(),MobClickHelper.DIS_BAG_ALL);

                mContext.startActivity(intentList);
                break;
        }
    }

    public void setCategoryId(String category_id) {
        this.category_id = category_id;
        if (mAdapter != null){
            mAdapter.setCategoryId(category_id);
        }
    }

    public void setF4(List<RecommendData.DataBean.ToolBean> tool) {
        ll_nav_4.setVisibility(VISIBLE);

        for (int i=0;i<tool.size();i++){
            View inflate = LayoutInflater.from(mContext).inflate(R.layout.f4, ll_nav_4, false);
            LinearLayout ll_everyday_new = (LinearLayout) inflate.findViewById(R.id.ll_everyday_new);
            ImageView iv_everyday_new = (ImageView) inflate.findViewById(R.id.iv_everyday_new);
            TextView tv_everyday_new = (TextView) inflate.findViewById(R.id.tv_everyday_new);
            RecommendData.DataBean.ToolBean toolBean = tool.get(i);
            String icon = toolBean.getIcon();
            final String title = toolBean.getTitle();
            final String type = toolBean.getType();
            final String value = toolBean.getValue();
            final String is_login = toolBean.getIs_login();
            try {//防止加载慢导致的异常
                Glide.with(mContext).load(icon).into(iv_everyday_new);
            }catch (Exception e){

            }
            tv_everyday_new.setText(title);
            ll_everyday_new.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    MobclickAgent.onEvent(mContext, MobClickHelper.DIS_NAVI_POS, title);
                    HashMap<String, Object> temp = new HashMap<>();
                    temp.put("发现页面四个icon", title);
                    ZhugeSDK.getInstance().track(mContext, MobClickHelper.DIS_NAVI_POS, temp);

                    if (TextUtils.equals("0",is_login)){

                    }else{
                        if (!SPUtils.isLogined()){
//                            Intent intent = new Intent(mContext, LoginRegistActivity.class);
//                            mContext.startActivity(intent);
                            showLoginDialog();


                            return;
                        }
                    }
                    if (TextUtils.equals("2",type)) {
                        Intent intent = new Intent(mContext, PlayDeatalActivity.class);
                        intent.putExtra(LIST_ID, value);
                        mContext.startActivity(intent);
                    }else if (TextUtils.equals("3",type)){
                        if (TextUtils.equals("all_page",value)){
                            Intent intentList = new Intent(mContext, AllPlayerAcitivity.class);
                            if (TextUtils.isEmpty(category_id)){
                                category_id = "";
                            }
                            intentList.putExtra("category_id",category_id);
                            intentList.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                            //所有播单入口
//                MobclickAgent.onEvent(mContext.getApplicationContext(), MobClickHelper.DIS_BAG_ALL);
//                ZhugeSDK.getInstance().track(mContext.getApplicationContext(),MobClickHelper.DIS_BAG_ALL);

                            mContext.startActivity(intentList);
                        }else if (TextUtils.equals("232",value)){
                            if (!SPUtils.isLogined()){
//                            Intent intent = new Intent(mContext, LoginRegistActivity.class);
//                            mContext.startActivity(intent);
                                showLoginDialog();


                                return;
                            }
                            mContext.startActivity(new Intent(mContext, RecordActivity.class));
                        }else if (TextUtils.equals("文字转语音",value)){
                            if (!SPUtils.isLogined()){
//                            Intent intent = new Intent(mContext, LoginRegistActivity.class);
//                            mContext.startActivity(intent);
                                showLoginDialog();


                                return;
                            }
                            mContext.startActivity(new Intent(mContext, TextToVoiceActivity.class));
                        }else if (TextUtils.equals("app_nicetease",value)){
                            Intent intent = new Intent(mContext, GoodListenerTeaseActivity.class);
                            mContext.startActivity(intent);
                        }

                    }
                }
            });
            ll_nav_4.addView(inflate);
            if (i != tool.size()-1){
                TextView view = new TextView(mContext);
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,10,1);
                ll_nav_4.addView(view,params);
            }
        }

    }

    private void showLoginDialog() {
        AlertDialog alertDialog = new AlertDialog.Builder(mContext)
                .setTitle("请登录后使用")
                .setMessage("由于功能升级，该语音需登录并上传后才可使用")
                .setPositiveButton("去登录", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(mContext, LoginRegistActivity.class);
                        mContext.startActivity(intent);
                        dialog.dismiss();
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                }).create();
        alertDialog.show();
    }
}
